package m.jclecai.com.activity;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.UserBean;
import m.jclecai.com.constant.Constant_Result;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.dialog.SelectlistDialog;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.utils.http.responseBody.AddBankBody;

/**
 * Created by ZD on 2017/5/22.
 */

public class BindbankCardActivity extends BaseActivity {

    UserBean userBean;
    @BindView(R.id.tv_no)
    TextView tvNo;
    @BindView(R.id.tv_bank)
    TextView tvBank;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.tv_khzh)
    TextView tvKhzh;
    @BindView(R.id.ll_show)
    LinearLayout llShow;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_khyh)
    TextView tvKhyh;
    @BindView(R.id.sp_khyh)
    Spinner spKhyh;
    @BindView(R.id.tv_province)
    TextView tvProvince;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.et_card)
    EditText etCard;
    @BindView(R.id.et_khh)
    EditText etKhh;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.ll_bind)
    LinearLayout llBind;




    @Override
    public int getContentView() {
        return R.layout.activity_bind_bank;
    }

    @Override
    public void initView() {
        super.initView();
        setTitle("绑定银行卡");
        leftOptionText("");
        userBean = PreferenceUtil.readUser(this);
    }

    @Override
    public void initData() {
        super.initData();
        userBean = PreferenceUtil.readUser(this);
        //这里我们做一个判断，看看用户是否有真实的姓名的，
        // 如果没有的话，我们就跳转到，设置真实姓名的页面；

        if (TextUtils.isEmpty(userBean.getRealname())) {
            DisPlay("请先绑定身份证!!!");
            Intent intent = new Intent(this, BindIDCardActivity.class);
            startActivityForResult(intent, Constant_Result.BINDCARD);
        }

        tvName.setText(userBean.getRealname());
//        llBind.setVisibility(View.GONE);
        llShow.setVisibility(View.GONE);
//        if (userBean != null && !TextUtils.isEmpty(userBean.getBankno())) {
//            tvNo.setText(userBean.getBankno());
//            tvBank.setText(userBean.getBank());
//            tvName.setText(userBean.getRealname());
//            tvArea.setText("");
//            tvKhzh.setText("");
//        }
//        getBank();
    }

    @OnClick(R.id.iv_selest_card)
    void ivSelestCard() {
        SelectlistDialog dialog = new SelectlistDialog(this, true, SelectlistDialog.TYPE.BANK,
                tvKhyh.getText().toString(), "");
        dialog.setTitle("选择银行");
        dialog.setListener(new Callback() {

            @Override
            public void callback(String o) {
                super.callback(o);
                tvKhyh.setText(o.toString());
            }
        });
        dialog.show();
    }

    @OnClick(R.id.iv_selest_area)
    void ivSelestArea() {
        SelectlistDialog dialog = new SelectlistDialog(this, true, SelectlistDialog.TYPE.CITY,
                "", "");
        dialog.setTitle("选择开户地");
        dialog.setListener(new Callback() {
            @Override
            public void callback(String o) {
                super.callback(o);
                String s = o.toString();
                String[] split = s.split("_");
                tvProvince.setText(split[0]);
                tvCity.setText(split[1]);
            }
        });
        dialog.show();
    }

    //上一个版本需要调用 确认下来会删除此代码
    @Deprecated
    void getBank() {
        llShow.setVisibility(View.GONE);
        llBind.setVisibility(View.GONE);
        Map<String, String> map = new HashMap<>();
        map.put("user", userBean.getUsername());
        map.put("password", PreferenceUtil.getPassword(this));
        showDialog();
        HttpManager.postAsyn(Constant_Url.GET_USER_BANK, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                MyToast.showToast(BindbankCardActivity.this, getString(R.string.error_network));
                llShow.setVisibility(View.VISIBLE);
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                JSONObject proofBean = FastOk.get(response, JSONObject.class);
                if (proofBean != null) {
                    if (0 == (proofBean.getIntValue("status"))) {
                        tvNo.setText(proofBean.getString("bankno"));
                        tvBank.setText(proofBean.getString("bank"));
                        tvArea.append(proofBean.getString("bankprovince"));
                        tvArea.append("  ");
                        tvArea.append(proofBean.getString("bankcity"));
                        tvKhzh.setText(proofBean.getString("bankbranch"));
                        llShow.setVisibility(View.VISIBLE);
                    } else {
                        //绑定
                        llBind.setVisibility(View.VISIBLE);
                    }
                } else {
                    MyToast.showToast(BindbankCardActivity.this, getString(R.string.error_network));
                    llShow.setVisibility(View.VISIBLE);
                }
            }
        }, map);
    }

    //绑定身份证
    @OnClick(R.id.tv_sure)
    void bingCard() {
        //开户银行
        final String khyh = tvKhyh.getText().toString();
        //省
        final String province = tvProvince.getText().toString();
        //市
        String city = tvCity.getText().toString();
        //卡号
        final String card = etCard.getText().toString();
        //开户行
        String khh = etKhh.getText().toString();

        //这里是使用Spanner的时候我们需要的；
        /*if (spKhyh.getSelectedItemPosition() == 0) {
            MyToast.showToast(this, "请选择开户银行");
            return;
        }*/
        if (TextUtils.isEmpty(card)) {
            MyToast.showToast(this, "请输入银行卡号");
            return;
        }
        if (TextUtils.isEmpty(khh)) {
            MyToast.showToast(this, "请输入开户行");
            return;
        }
        if (card.length() < 16) {
            MyToast.showToast(this, "请输入正确的银行卡号");
            return;
        }
        showDialog();
        Map<String, String> map = new HashMap<>();
        map.put("user", userBean.getUsername());
        map.put("password", PreferenceUtil.getPassword(this));
       // map.put("bank", spKhyh.getSelectedItem().toString());//khyh
        map.put("bank", khyh);//khyh
        map.put("bankNumber", card);
        map.put("bankno", card);
        map.put("province", province);
        map.put("city", city);
        map.put("subbranch", khh);

        HttpManager.postAsyn(Constant_Url.SET_USER_BANK, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                MyToast.showToast(BindbankCardActivity.this, getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                AddBankBody body = FastOk.get(response, AddBankBody.class);
                if (body != null) {
                    MyToast.showToast(BindbankCardActivity.this, body.getMessage());
                    if (body.getStatus() == 0) {
                        userBean.setBankno(card);
                        userBean.setBank(khyh);
                        PreferenceUtil.writeUser(BindbankCardActivity.this, userBean);
//                        BankBean defaultWithDrawBank = PreferenceUtil.getDefaultWithDrawBank(BindbankCardActivity.this);
//                        if(defaultWithDrawBank == null)
                        PreferenceUtil.setDefaultWithDrawBank(BindbankCardActivity.this,body.getRows());
                        setResult(RESULT_OK);
                        finish();
                    }
                } else {
                    MyToast.showToast(BindbankCardActivity.this, getString(R.string.error_network));
                }
            }
        }, map);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == Constant_Result.BINDCARD){
            if(resultCode == RESULT_OK){
                //这里我们要执行的逻辑就是：
                UserBean userBean = PreferenceUtil.readUser(this);
                tvName.setText(userBean.getRealname());
            }else{
                finish();
            }
        }
    }
}
